package com.woniu.product;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class DelayProduct implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String msg,String routeKey,int ttl){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
        rabbitTemplate.convertAndSend("customExchange", routeKey, msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(ttl);//设置消息的ttl时间
                return message;
            }
        });
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        System.out.println("===============================");
        System.out.println(correlationData);
        System.out.println(b);
        System.out.println(s);
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        System.out.println("==============================");
        System.out.println(returnedMessage);
    }
}
